package fr.ants.controller.command;

import fr.ants.controller.clock.Clock;
import fr.ants.view.panel.Canvas;
import fr.ants.view.panel.TabbedPane;

/**
 * Command to add a new Resource.
 * 
 * @author Jordan ENSFELDER
 * 
 */
public class NewObstacleCommand implements Command {

	/**
	 * Canvas.
	 */
	private final Canvas canvas;

	/**
	 * Clock.
	 */
	private final Clock clock;

	/**
	 * TabbedPane.
	 */
	private TabbedPane tabbedPane;

	/**
	 * Build
	 * 
	 * @param canvas
	 *            Canvas.
	 * @param clock
	 *            Clock.
	 */
	public NewObstacleCommand(final Canvas canvas, final Clock clock) {
		this.canvas = canvas;
		this.clock = clock;
	}

	/**
	 * we need the tabbedpane to execute this command.
	 * 
	 * @param tabbedPane
	 *            tabbedpane
	 */
	public void setTabbedPane(final TabbedPane tabbedPane) {
		this.tabbedPane = tabbedPane;
	}

	@Override
	public void execute() {
		if (canvas.getMousePosition() != null) {
			int pDifficulty = tabbedPane.getSimulationPanel()
					.getDifficultyValue();
			int pSize = tabbedPane.getSimulationPanel().getSizeValue();

			clock.getSimulator()
					.getGrid()
					.addObstacle(canvas.getSelectedGridX(),
							canvas.getSelectedGridY(), pDifficulty, pSize);

			if (!clock.isRunning()) {
				canvas.repaint();
			}

		}
	}

}
